package com.tanzeer.deliverytracker.functionality;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.tanzeer.deliverytracker.R;

public class Main extends ListActivity {
	
	private File mCurrentNode = null;
	private File mLastNode = null;
	private File mRootNode = null;
	private ArrayList<File> mFiles = new ArrayList<File>();
	private CustomAdapter mAdapter = null;
	private String mCaller = "";
    
	private Button mSaveButton;
	private Button mCancelButton;
	private LinearLayout mButtonsLayout;
	ActionBar actionbar;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        actionbar = getActionBar();
        actionbar.setTitle(R.string.importdbfileb);
        mSaveButton = (Button)findViewById(R.id.button1);
        mCancelButton = (Button)findViewById(R.id.button2);
        mButtonsLayout = (LinearLayout)findViewById(R.id.buttonslayout);
        mButtonsLayout.setVisibility(View.GONE);
        
        ActionBar ac = getActionBar();
        ac.setHomeButtonEnabled(true);
        ac.setDisplayHomeAsUpEnabled(true);
        Intent i = getIntent();
		if (i != null) {
			mCaller = i.getStringExtra("caller");
		}
        
		if (mCaller != null && mCaller.equalsIgnoreCase("savedb")){
			actionbar.setTitle(R.string.savedbfileb);
			mButtonsLayout.setVisibility(View.VISIBLE);
		}
		
		
		
		
        mAdapter = new CustomAdapter(this, R.layout.list_row, mFiles);
	    setListAdapter(mAdapter);
	    if (savedInstanceState != null) {
	    	mRootNode = (File)savedInstanceState.getSerializable("root_node");
	    	mLastNode = (File)savedInstanceState.getSerializable("last_node");
	    	mCurrentNode = (File)savedInstanceState.getSerializable("current_node");
	    }
	    refreshFileList();
	    
		mSaveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mCaller != null && mCaller.equalsIgnoreCase("savedb")) {
					Intent resultIntent = new Intent();
					resultIntent.putExtra("saveDbToPath", mCurrentNode.getAbsolutePath());
					setResult(Activity.RESULT_OK, resultIntent);
					finish();
				}
			}
		});

		mCancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
    }
    
    private void refreshFileList() {
		if (mRootNode == null) mRootNode = new File(Environment.getExternalStorageDirectory().toString());
		if (mCurrentNode == null) mCurrentNode = mRootNode; 
		mLastNode = mCurrentNode;
		File[] files = mCurrentNode.listFiles();
		mFiles.clear();
		mFiles.add(mRootNode);
		mFiles.add(mLastNode);
		if (files!=null) {
			for (int i = 0; i < files.length; i++) {
				if (mCaller.equalsIgnoreCase("savedb")) {
					if (files[i].isDirectory()) {
						mFiles.add(files[i]);
					}
				} else if (mCaller.equalsIgnoreCase("importdb")) {
					mFiles.add(files[i]);
				}

			}
		}
		Collections.sort(mFiles);
		mAdapter.notifyDataSetChanged();
	}
    
    @Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putSerializable("root_node", mRootNode);
		outState.putSerializable("current_node", mCurrentNode);
		outState.putSerializable("last_node", mLastNode);
		super.onSaveInstanceState(outState);
	} 
    
    /**
     * Listview on click handler.
     */
	@Override
	public void onListItemClick(ListView parent, View v, int position, long id){   
		File f = (File) parent.getItemAtPosition(position);
		if (position == 1) {
			if (mCurrentNode.compareTo(mRootNode)!=0) {
				mCurrentNode = f.getParentFile();
				refreshFileList();
			}
		} else if (f.isDirectory()) {
			mCurrentNode = f;
			refreshFileList();
		} else {
			if (mCaller != null && mCaller.equalsIgnoreCase("importdb")) {
				Intent resultIntent = new Intent();
				resultIntent.putExtra("fileToImportPath", f.getAbsolutePath());
				setResult(Activity.RESULT_OK, resultIntent);
				finish();
			}
			
		}
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {

	    switch (item.getItemId()) {
	    case android.R.id.home:
	    	finish();
	        break;

	    default:
	        break;
	    }
	    return super.onOptionsItemSelected(item);
	}
}